<template>
  <div class="goods">
    <div class="menu-wrapper" ref="menuWrapper">
      <ul>
        <li v-for="(item, index) in goods.goods" :key="index" class="menu-item" :class="{'current':currentIndex === index}" @click="selectMenu(index,$event)">
          <span class="text">
            <span v-show="item.type>0" class="icon" :class="classMap[item.type]"></span>{{item.name}}
          </span>
        </li>
      </ul>
    </div>
    <div class="foods-wrapper" ref="foodsWrapper">
      <ul>
        <li v-for="(item,index) in goods.goods" :key="index" class="food-list food-list-hook">
          <h1 class="title">{{ item.name }}</h1>
          <ul>
            <li v-for="(food,index) in item.foods" :key="index" class="food-item">
              <div class="icon">
                <img :src="food.icon" width="57px" height="57px">
              </div>
              <div class="content">
                <h2 class="name">{{food.name}}</h2>
                <p class="desc">{{food.description}}</p>
                <div class="extra">
                  <span class="count">月售{{food.sellCount}}份</span>
                  <span>好评率{{food.rating}}%</span>
                </div>
                <div class="price">
                  <span class="now">￥{{food.price}}</span>
                  <span v-show="food.oldPrice" class="old">￥{{food.oldPrice}}</span>
                </div>
              </div>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <shopcart></shopcart>
  </div>
</template>

<script>
  import BScroll from 'better-scroll';
  import shopcart from '../shopcart/shopcart.vue';
  export default {
    props: {
      seller: Object
    },
    data() {
      return {
        goods: [],
        listHeight: [],
        scrollY: 0
      };
    },
    methods: {
      selectMenu(index,event) {
        if(!event._constructed){
          return;
        }
      },
      _initScroll() {
        this.menuScroll = new BScroll(this.$refs.menuWrapper,{
          click: true
        });
        this.foodsScroll = new BScroll(this.$refs.foodsWrapper,{
          probeType: 3
        });
        this.foodsScroll.on('scroll', (pos) => {
          this.scrollY = Math.abs(Math.round(pos.y));
        });
      },
      _calculateHeight() {
        let foodList = this.$refs.foodsWrapper.getElementsByClassName('food-list-hook'),
            height = 0;
        this.listHeight.push(height);
        for (let i = 0; i < foodList.length; i ++) {
          let item = foodList[i];
          height += item.clientHeight;
          this.listHeight.push(height);
        }
      }
    },
    components: {
      shopcart
    },
    computed: {
      currentIndex() {
        for(let i = 0; i <this.listHeight.length; i++) {
          let height1 = this.listHeight[i];
          let height2 = this.listHeight[i + 1];
          if(!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
            return i;
          }
        }
        return 0;
      }
    },
    created() {
      this.$http.get('https://dsn.apizza.net/mock/6a800a457cff8471abd6177620ca593c/goods').then((response) => {
        this.goods = response.body;
        this.$nextTick(() => {
          this._initScroll();
          this._calculateHeight();
        })
      });
      this.classMap = ["decrease", "discount", "special", "invoice", "guarantee"];
    }
  }
</script>

<style lang="less" scoped>
  .goods {
    display: flex;
    position: absolute;
    top: 178px;
    bottom: 46px;
    width: 100%;
    overflow: hidden;
    .menu-wrapper {
      flex: 0 0 80px;
      width: 80px;
      background: #f3f5f7;
      .menu-item {
        display: table;
        height: 54px;
        width: 56px;
        line-height: 14px;
        padding: 0 12px;
        .icon {
          display: inline-block;
          width: 12px;
          height: 12px;
          margin-right: 4px;
          background-size: 12px 12px;
          vertical-align: top;
        }
        .decrease {
          background-image: url("./images/decrease_3@3x.png");
        }
        .discount {
          background-image: url("./images/discount_3@3x.png");
        }
        .special {
          background-image: url("./images/special_3@3x.png");
        }
        .invoice {
          background-image: url("./images/invoice_3@3x.png");
        }
        .guarantee {
          background-image: url("./images/guarantee_3@3x.png");
        }
        .text {
          display: table-cell;
          width: 56px;
          vertical-align: middle;
          font-size: 12px;
          color: rgba(7, 17, 27, 0.6);
          border-bottom: 1px solid rgba(7,17,27, 0.1);
        }
      }
      .menu-item:last-child .text {
        border-bottom: none;
      }
      .current {
        position: relative;
        margin-top: -1px;
        z-index: 10;
        background-color: #fff;
        .text {
          border-bottom: none;
          color: rgba(7, 17, 27, 1);
          font-weight: 700;
        }
      }
    }
    .foods-wrapper {
      flex: 1;
      .title {
        padding-left: 14px;
        height: 26px;
        line-height: 26px;
        border-left: 2px solid #d9dde1; 
        font-size: 12px;
        color: rgba(7, 17, 27, 0.4);
        background-color: #f3f5f7;
      }
      .food-item {
        display: flex;
        margin: 18px;
        padding-bottom: 18px;
        border-bottom: 1px solid rgba(7,17,27, 0.1);
        .icon {
          flex: 0 0 57px;
          margin-right: 10px;
        }
        .content {
          flex: 1;
          .name {
            margin: 2px 0 8px 0;
            height: 14px;
            line-height: 14px;
            font-size: 14px;
            color: rgba(7, 17, 27, 1)
          }
          .desc {
            margin-bottom: 8px;
            line-height: 12px;
            font-size: 10px;
            color: rgba(7, 17, 27, 0.4);
          }
          .extra {
            margin-bottom: 8px;
            line-height: 10px;
            font-size: 10px;
            color: rgba(7, 17, 27, 0.4);
            .count {
              margin-right: 12px;
            }
          }
          .price {
            font-weight: 700;
            line-height: 24px;
            .now {
              margin-right: 8px;
              font-size: 14px;
              color: rgba(240, 20, 20, 1);
            }
            .old {
              text-decoration: line-through;
              font-size: 10px;
              color: rgba(7,17,27, 0.1);
            }
          }
        }
      }
      .food-item:last-child {
        margin-bottom: 0;
        border-bottom: 0;
      }
    }
  }
</style>
